	function Communicator(source){
	this.contentType  = 'application/x-www-form-urlencoded';
	this.parameters = new Array();
	this.method = 'POST';
	this.output = null;

	if(source) this.source = source;
	else this.source = globalServerURL+'listener.php';
}

Communicator.prototype.setSource = function(source){
	this.source = source;
}

Communicator.prototype.addParameter = function(name, value){
	if(value != null){ // Enviaram nome e valor do parâmetro
		this.parameters[name] = value;
	}else if(isString(name)){ // String como nome=valor
		if(name.indexOf('=') != -1){
			var paramsParts = name.split('=');
			this.parameters[paramsParts[0]] = paramsParts[1];	
		}else{
			this.parameters[name] = 1;
		}		
	}
}

Communicator.prototype.setParameter = function(name, value){
	this.addParameter(name, value);
}

Communicator.prototype.resetParameters = function(){
	this.parameters = new Array();
}

Communicator.prototype.sendRequest = function(callback){
	this.output = null;

    var request = this.returnXmlHttpRequestObject();
	var self = this; //to be accessed inside the clousures

    request.onreadystatechange = function(){
        if(request.readyState == 4 && request.status == 200) { //when it's ready
			//whether it has a callback function
            if(callback && typeof callback === 'function') callback(request.responseText);
			else self.output = request.responseText;
        }
    };
	
    if(this.parameters != null){ //sends to an external server
    	var parametersString = this.getParametersString();
		request.open(this.method, this.source, true);
		request.setRequestHeader("Content-type", this.contentType);
		request.setRequestHeader("Content-length", parametersString.length);
		request.send(parametersString);
	}
}

Communicator.prototype.returnXmlHttpRequestObject = function(){
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if(window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        alert("erro: Este navegador tem dificuldades para se comunicar com o servidor. Considere atualizá-lo.");
    }
}

Communicator.prototype.getOutput = function(){
	return eval(this.output);
}

Communicator.prototype.setParameterList = function(list){
	for(var key in list) this.parameters[key] = list[key];
}

Communicator.prototype.getParametersString = function(){
	var string = '';
	var counter = 0;
	for(var key in this.parameters){
		if(counter > 0) string += '&';
		string += key+'='+this.parameters[key];
		counter++;
	}
	
	return string;
}

Communicator.prototype.alert = function(){
	alert(this.getParametersString());
}

Communicator.prototype.hasParameter = function(){
	for(var key in this.parameters) return true;
	return false;
}